package TCP.UDP.Test;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Client {
    public static void main(String[] args) throws Exception {
        //封装一个数据包
        String msg = "今天天气不错！";
        byte[] b = msg.getBytes();
        InetAddress address = InetAddress.getByName("localhost");//本机地址
        DatagramPacket dp = new DatagramPacket(b,0,b.length,address,8888);
        //发送
        DatagramSocket ds = new DatagramSocket();
        ds.send(dp);



        byte[] a = new byte[1024];
        DatagramPacket dp1 = new DatagramPacket(a,a.length);
        System.out.println("服务器启动成功~");
        ds.receive(dp1);
        System.out.println("客户端"+dp.getSocketAddress()+"说：");
        System.out.println(new String(dp1.getData(),0,dp1.getLength()));
    }
}
